/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view.panels;

import com.jtattoo.plaf.acryl.AcrylBorders;
import control.OrderController;
import handler.HandlerFactory;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import model.Menu;
import utility.ImageToolbox;
import view.MainGUI;

/**
 *
 * @author Jeppe
 */
public class MenuPanel extends javax.swing.JPanel {

    private final MainGUI mainGUI;
    private Calendar chosenDate;
    private final static boolean DEBUGMODE = true;

    /**
     * Creates new form ProduktListePanel
     *
     * @param mainGUI
     */
    public MenuPanel(MainGUI mainGUI) {
        this.mainGUI = mainGUI;
        initComponents();
        chosenDate = Calendar.getInstance();
        kurvKnap.setOpaque(true);
        kurvKnap.setOpaque(false);
        kurvKnap.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        //<editor-fold defaultstate="collapsed" desc="Timer til opdatering af dateCombo">
        setDateCombo();
        Timer t = new Timer(10000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                setDateCombo();
            }
        });
        t.start();
//</editor-fold>
    }
    private boolean settingDateCombo = false;

    private void setDateCombo() {
        settingDateCombo = true;
        SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy");

        if (DEBUGMODE) {
            System.out.println("Idag er det: " + SDF.format(chosenDate.getTime()));
        }
        dateCombo.removeAllItems();
        Calendar calEnd = Calendar.getInstance();
        SimpleDateFormat yearFormatter = new SimpleDateFormat("yyyy");
        SimpleDateFormat dateFormatter = new SimpleDateFormat("dd");
        calEnd.set(Integer.parseInt(yearFormatter.format(calEnd.getTime())), Calendar.MONTH, Integer.parseInt(dateFormatter.format(calEnd.getTime())), OrderController.DEADLINETIME, OrderController.DEADLINEMINUT);
        if (calEnd.getTimeInMillis() < chosenDate.getTimeInMillis()) {
            System.out.println(SDF.format(calEnd.getTime()));
            System.out.println("Kl er over 21:00");
            for (int i = 0; i < 7; i++) {
                chosenDate.add(Calendar.DAY_OF_MONTH, 1);
                dateCombo.addItem(SDF.format(chosenDate.getTime()));
            }
        } else {
            for (int i = 0; i < 7; i++) {
                dateCombo.addItem(SDF.format(chosenDate.getTime()));
                chosenDate.add(Calendar.DAY_OF_MONTH, 1);
            }
        }
        settingDateCombo = false;
        setChosenDateFromCombo();
    }

    private void opdaterMenu() {
        Calendar cal = Calendar.getInstance();
        ArrayList<ProduktListePanel> produktListePanelList = new ArrayList<>();
        try {
            Menu enMenu = HandlerFactory.getInstance().getMenuHandler().getMenuByDate("22032014");
            for (int i = 0; i < enMenu.getProduktList().size(); i++) {
                produktListePanelList.add(new ProduktListePanel(this, enMenu.getProduktList().get(i)));
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Fejl ved hentning af menu på dags dato");
        }
        int locX = 2;
        int locY = 2;
        for (int i = 0; i < produktListePanelList.size(); i++) {
            ProduktListePanel PLP = produktListePanelList.get(i);
            menuListePanel.add(PLP);
            PLP.setBounds(locX, locY, menuListePanel.getWidth() - 4, 87);
            PLP.setVisible(true);

            locY = locY + 5 + 87;
            menuListePanel.setPreferredSize(new Dimension(menuListePanel.getWidth(), locY));
        }
        menuListePanel.repaint();
        revalidate();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        kurvKnap = new javax.swing.JButton(){
            public void paint (Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)kurvKnap.getIcon(), 0, 0, kurvKnap.getWidth(), kurvKnap.getHeight());
            }
        }
        ;
        jLabel3 = new javax.swing.JLabel();
        antalVarer = new javax.swing.JLabel();
        menuListeScrollPane = new javax.swing.JScrollPane();
        menuListePanel = new javax.swing.JPanel();
        dateCombo = new javax.swing.JComboBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();

        setOpaque(false);
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });
        setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(null);

        kurvKnap.setBackground(new java.awt.Color(204, 204, 204));
        kurvKnap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/shopping_basket.png"))); // NOI18N
        kurvKnap.setContentAreaFilled(false);
        kurvKnap.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                kurvKnapMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                kurvKnapMouseExited(evt);
            }
        });
        kurvKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kurvKnapActionPerformed(evt);
            }
        });
        jPanel1.add(kurvKnap);
        kurvKnap.setBounds(120, 10, 40, 40);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Varer i kurven:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(5, 20, 80, 14);

        antalVarer.setText("0");
        jPanel1.add(antalVarer);
        antalVarer.setBounds(90, 18, 20, 20);

        add(jPanel1);
        jPanel1.setBounds(460, 30, 170, 60);

        menuListeScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        menuListePanel.setBackground(new java.awt.Color(51, 255, 0));
        menuListePanel.setOpaque(false);
        menuListePanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                menuListePanelMouseEntered(evt);
            }
        });
        menuListePanel.setLayout(null);
        menuListeScrollPane.setViewportView(menuListePanel);

        add(menuListeScrollPane);
        menuListeScrollPane.setBounds(0, 20, 450, 410);

        dateCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                dateComboItemStateChanged(evt);
            }
        });
        add(dateCombo);
        dateCombo.setBounds(460, 5, 170, 20);

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Varme Retter");
        jCheckBox1.setOpaque(false);
        add(jCheckBox1);
        jCheckBox1.setBounds(80, 0, 90, 23);

        jCheckBox2.setSelected(true);
        jCheckBox2.setText("Sandwich");
        jCheckBox2.setOpaque(false);
        add(jCheckBox2);
        jCheckBox2.setBounds(0, 0, 70, 23);
    }// </editor-fold>//GEN-END:initComponents

    private boolean opdateret = false;
    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
        if (!opdateret) {
            opdaterMenu();
            opdateret = true;
        }
    }//GEN-LAST:event_formMouseMoved

    private void kurvKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kurvKnapActionPerformed
        mainGUI.showKurv();
    }//GEN-LAST:event_kurvKnapActionPerformed

    private void menuListePanelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menuListePanelMouseEntered
        if (!opdateret) {
            opdaterMenu();
            opdateret = true;
        }
    }//GEN-LAST:event_menuListePanelMouseEntered

    private void kurvKnapMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_kurvKnapMouseEntered
        kurvKnap.setOpaque(true);
    }//GEN-LAST:event_kurvKnapMouseEntered

    private void kurvKnapMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_kurvKnapMouseExited
        kurvKnap.setOpaque(false);
    }//GEN-LAST:event_kurvKnapMouseExited

    private void dateComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_dateComboItemStateChanged
        if (!settingDateCombo) {
            setChosenDateFromCombo();
            SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy");
            if (DEBUGMODE) {
                System.out.println(SDF.format(chosenDate.getTime()));
            }
        }
    }//GEN-LAST:event_dateComboItemStateChanged

    private void setChosenDateFromCombo() {
        SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy");
        try {
            chosenDate.setTime(SDF.parse((String) dateCombo.getSelectedItem()));
        } catch (ParseException ex) {
            System.out.println("Der er sket en fejl ved chosen date" + ex.getLocalizedMessage());
        }
    }

    public Calendar getSelectedDate() {
        return chosenDate;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel antalVarer;
    private javax.swing.JComboBox dateCombo;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton kurvKnap;
    private javax.swing.JPanel menuListePanel;
    private javax.swing.JScrollPane menuListeScrollPane;
    // End of variables declaration//GEN-END:variables
}
